package com.cqw.datamodel.sort;

import java.util.Arrays;

/**
 * @Author: caoqingwen
 * @CreateTime: 2021-11-16 15:32
 * @Description: 选择排序
 */
public class Selection {

    public static void main(String[] args) {
        int[] arr = {4, 5, 8, 7, 9, 2, 1};

        for (int i = 0; i < arr.length; i++) {
            //设置本次遍历为最小值
            int minIndex = i;
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[minIndex] > arr[j]) {
                    minIndex = j;
                }
            }
            if (i != minIndex) {
                int temp = arr[minIndex];
                arr[minIndex] = arr[i];
                arr[i] = temp;
            }
        }
        System.out.println(Arrays.toString(arr));


    }

}
